package com.example.tests;

import com.example.utils.SortedListOf;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 07.03.13
 * Time: 11:44
 * To change this template use File | Settings | File Templates.
 */
public class GroupRemovalTestSet extends TestBase {

    @BeforeClass
    public void prepare(){
        applicationManager.navigateTo()
                .mainPage()
                .groupsList();
    }

    @Test
    public void deleteSomeGroup(){
        //save old state
        SortedListOf<GroupData> oldGroupList = applicationManager.getModel().getGroupList();

        //get groups number and check if there are groups
        int groupCount = oldGroupList.size();
        if (groupCount == 0)
            return;

        //pick an object to modify
        int randomGroupNumber = applicationManager.getGroupHelper().getRandomIndex(groupCount);

        //actions
        applicationManager.getGroupHelper().deleteGroup(randomGroupNumber);

        //save new state
        SortedListOf<GroupData> newGroupList = applicationManager.getModel().getGroupList();

        //compare states
        assertThat(newGroupList, equalTo(oldGroupList.without(randomGroupNumber)));

        //compare data with model
        if (shouldCheck()){
            if("yes".equals(applicationManager
                    .getProperty("check.db"))){
                assertThat(applicationManager.getModel().getGroupList()
                        , equalTo(applicationManager.getHibernateHelper().listGroups()));
            }

            if("yes".equals(applicationManager
                    .getProperty("check.ui"))){
                assertThat(applicationManager.getModel().getGroupList()
                        , equalTo(applicationManager.getGroupHelper().getUiGroupList()));
            }
        }
    }
}
